<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">

    <!--搜索表单-->
    <el-form ref="form3" :inline="true" :model="brand" class="demo-form-inline">

        <el-form-item label="当前状态" prop="status">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称" prop="companyName">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称" prop="brandName">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="selectAll">查询</el-button>
            <el-button type="primary" @click="cz">重置</el-button>
        </el-form-item>
    </el-form>

    <!--按钮-->

    <el-row>

        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="resetForm">新增</el-button>

    </el-row>
    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisible"
            width="30%"
    >

        <el-form ref="form1" :model="brand2" label-width="80px">
            <el-form-item label="品牌名称" prop="brandName">
                <el-input v-model="brand2.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称" prop="companyName">
                <el-input v-model="brand2.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序" prop="ordered">
                <el-input v-model="brand2.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注" prop="description">
                <el-input type="textarea" v-model="brand2.description"></el-input>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-switch v-model="brand2.status"
                           :active-value="1"
                           :inactive-value="0"
                ></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="addbrand">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <!--修改数据对话框表单-->
    <el-dialog
            title="修改品牌"
            :visible.sync="dialogVisible2"
            width="30%"
    >

        <el-form ref="form2" :model="brand2" label-width="80px">
            <el-form-item label="品牌名称" prop="brandName">
                <el-input v-model="brand2.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称" prop="companyName">
                <el-input v-model="brand2.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序" prop="ordered">
                <el-input v-model="brand2.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注" prop="description">
                <el-input type="textarea" v-model="brand2.description"></el-input>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-switch v-model="brand2.status"
                           :active-value="1"
                           :inactive-value="0"
                ></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="updateBrand">提交</el-button>
                <el-button @click="dialogVisible2 = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" @click="update1(scope.row.id)">修改</el-button>
                    <el-button type="danger" @click="deleteById(scope.row.id)">删除</el-button>
                </template>

            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>


<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<script src="js/axios-0.18.0.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        mounted() {
            this.selectAll()
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex % 4 == 1) {
                    return 'warning-row';
                } else if (rowIndex % 4 == 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 查询方法
            //分页查询所有
            selectAll() {
                axios.post(
                    "http://192.168.138.100:80/brand/selectByPageAndCondition/" + this.currentPage + "&" + this.pageSize
                    , this.brand
                ).then(res => {
                    this.totalCount = res.data.totalCount
                    this.tableData = res.data.rows
                    console.log(this.tableData)
                })
            },
            //删除按键
            deleteById(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    axios.delete("http://192.168.138.100:80/brand/deleteById?id=" + id,
                    ).then(res => {
                        if (res.data == true) {
                            this.$message.success('删除成功');
                            // this.selectByPage()
                            this.selectAll()
                        } else {
                            this.$message.error('删除失败');
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //批量删除
            deleteByIds() {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        this.ids[i] = this.multipleSelection[i].id
                    }
                    axios.post("http://192.168.138.100:80/brand/deleteByIds",
                        this.ids
                    ).then(res => {
                        if (res.data == true) {
                            this.$message.success('删除成功');
                            // this.selectByPage()
                            this.selectAll()
                        } else {
                            this.$message.error('删除失败');
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //编辑按钮
            update1(id) {
                axios.get("http://192.168.138.100:80/brand/findById?id=" + id,
                ).then((res) => {
                        this.brand2 = res.data
                    }
                )
                this.dialogVisible2 = true
            },
            updateBrand() {
                this.dialogVisible2 = false
                axios.put("http://192.168.138.100:80/brand/update",
                    this.brand2
                ).then((res) => {
                        if (res.data == true) {
                            this.selectAll()
                            this.$message.success('修改成功');
                        } else {
                            this.$message.error('修改失败');
                        }
                    }
                )
            },
            //重置表单
            resetForm() {
                this.dialogVisible = true;
                // this.$refs[formName].resetFields();重置为初始值
                this.brand2 = {}
            },
            //搜索重置
            cz() {
                // this.$refs.form3.resetFields();
                this.brand = {}
                this.selectAll()
            },
            // 添加数据
            addbrand() {
                axios.post("http://192.168.138.100:80/brand/add",
                    this.brand2
                ).then((res) => {
                    if (res.data == true) {
                        this.dialogVisible = false
                        // this.selectByPage()
                        this.selectAll()
                        this.$message.success('添加成功');
                        this.$refs["brand2"].resetFields();
                    } else {
                        this.$message.error('添加失败');
                    }
                })
            },
            //分页
            handleSizeChange(val) {
                this.pageSize = val
                this.selectAll()
            },
            handleCurrentChange(val) {
                this.currentPage = val
                this.selectAll()
            }
        },
        data() {
            return {
                // 当前页码
                currentPage: 1,
                //每页展示条数
                pageSize: 5,
                //总条数
                totalCount: 0,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,
                // 修改数据对话框是否展示的标记
                dialogVisible2: false,
                // 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: "",
                    ordered: "",
                    description: ""
                },
                brand2: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: "",
                    ordered: "",
                    description: ""
                },
                // 复选框选中数据集合
                multipleSelection: [],
                ids: [],
                // 表格数据
                tableData: []
            }
        }
    })

</script>

</body>
</html>